package com.bnq.face;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ActivityUtils {
    /**
     *  hook反射方向检查
     **/
    public static void fixOrientation(Activity activity) {
        try {
            Class activityClass = Activity.class;
            Field mActivityInfoField = activityClass.getDeclaredField("mActivityInfo");
            mActivityInfoField.setAccessible(true);
            ActivityInfo activityInfo = (ActivityInfo) mActivityInfoField.get(activity);
            //设置屏幕不固定
            activityInfo.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
        } catch (Exception e) {
        }
    }

    /**
     * hook反射检查是否透明色或者悬浮
     **/
    public static boolean isTranslucentOrFloating(Activity activity) {
        boolean isTranslucentOrFloating = false;
        try {
            int[] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
            final TypedArray typedArray = activity.obtainStyledAttributes(styleableRes);
            Method method = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            method.setAccessible(true);
            isTranslucentOrFloating = (boolean) method.invoke(null, typedArray);
            method.setAccessible(false);
        } catch (Exception e) {
        }
        return isTranslucentOrFloating;
    }
}
